package soccer.view;

import javax.swing.AbstractAction;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

import soccer.model.matches.Match;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.SwingConstants;

import com.sun.corba.se.spi.orbutil.fsm.Action;

import java.awt.Color;

public class MatchSearchPanel extends JPanel {
	private JTextField matchDateText;
	

	private JRadioButton searchByTeamRadioButton;
	private JRadioButton searchByDateRadioButton;
	private JComboBox teamsComboBox;
	private JLabel searchOptionLabel;
	private JButton searchMatchButton;
	private JTable matchSearchScrollTable;
	private JLabel searchInfoLabel;
	private JScrollPane matchSearchScrollPane;
	private ActionListener goMatchDetailButtonActionListener;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Create the panel.
	 */
	public MatchSearchPanel() {
		setLayout(null);
		
		searchByTeamRadioButton = new JRadioButton("By Team");
		buttonGroup.add(searchByTeamRadioButton);
		searchByTeamRadioButton.setBounds(100, 55, 109, 23);
		searchByTeamRadioButton.setActionCommand("byTeam");
		searchByTeamRadioButton.setSelected(true);
		add(searchByTeamRadioButton);
		
		searchByDateRadioButton = new JRadioButton("By Date");
		buttonGroup.add(searchByDateRadioButton);
		searchByDateRadioButton.setBounds(260, 55, 109, 23);
		searchByDateRadioButton.setActionCommand("byDate");
		add(searchByDateRadioButton);
		
		teamsComboBox = new JComboBox();
		teamsComboBox.setBounds(194, 95, 98, 23);
		add(teamsComboBox);
		
		searchOptionLabel = new JLabel("Team Select :");
		searchOptionLabel.setBounds(100, 99, 80, 14);
		add(searchOptionLabel);
		
		matchDateText = new JTextField();
		matchDateText.setBounds(194, 96, 98, 20);
		add(matchDateText);
		matchDateText.setColumns(10);
		
		searchInfoLabel = new JLabel("");
		searchInfoLabel.setForeground(new Color(102, 0, 0));
		searchInfoLabel.setHorizontalAlignment(SwingConstants.CENTER);
		searchInfoLabel.setBounds(82, 180, 306, 23);
		add(searchInfoLabel);
		
		matchSearchScrollPane = new JScrollPane();
		matchSearchScrollPane.setBounds(25, 129, 403, 88);
		matchSearchScrollPane.setVisible(false);
		add(matchSearchScrollPane);
		
		matchSearchScrollTable = new JTable();
		matchSearchScrollTable.setForeground(new Color(0, 0, 0));
		matchSearchScrollTable.setFillsViewportHeight(true);
		matchSearchScrollTable.setColumnSelectionAllowed(true);
		matchSearchScrollTable.setCellSelectionEnabled(true);
		matchSearchScrollTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
			},
			new String[] {
				"Match Date", "Home Team", "Result", "Away Team", "Match Detaylar\u0131", "matchId", "matchDetailId"
			}
		));
		matchSearchScrollTable.getColumnModel().getColumn(4).setPreferredWidth(95);
		matchSearchScrollTable.removeColumn(matchSearchScrollTable.getColumnModel().getColumn(6));
		matchSearchScrollTable.removeColumn(matchSearchScrollTable.getColumnModel().getColumn(5));
		matchSearchScrollPane.setViewportView(matchSearchScrollTable);
		
		searchMatchButton = new JButton("Search Match");
		searchMatchButton.setBounds(25, 244, 143, 23);
		add(searchMatchButton);

	}
	
	
	public void addMatchToMatchTable(List<Match> matches){
		if(matches == null || matches.size() < 1){
			showMatchResultErrorInfo();
			return;
		}
		DefaultTableModel tableModel = (DefaultTableModel)matchSearchScrollTable.getModel();
        resetTeamTable(tableModel);
       
		for(int i = 0; i <  matches.size(); i++){
			Vector<Comparable> v = new Vector<Comparable>();
			Match match = matches.get(i);
			v.add((Comparable) match.getMatchDate());
			v.add((Comparable) match.getHomeTeam().getTeamName());
			v.add((Comparable) (match.getMatchDetail().getHomeSkor()+":"+match.getMatchDetail().getAwaySkor()));
			v.add((Comparable) match.getAwayTeam().getTeamName());
			v.add((Comparable) "go" );
			v.add((Comparable)match.getMatchId());
			v.add((Comparable)match.getMatchDetail().getMatchDetailId());
			ButtonColumn buttonColumn = new ButtonColumn(matchSearchScrollTable,  4);
			buttonColumn.addActionListenerEditButton(goMatchDetailButtonActionListener);
			buttonColumn.setCommand(match.getMatchDetail().getMatchDetailId());
			tableModel.addRow(v);
			
		}
	
		searchInfoLabel.setVisible(false);
		matchSearchScrollPane.setVisible(true);
	}
	public void resetTeamTable(DefaultTableModel model){
		while(model.getRowCount() > 0){
			model.removeRow(0);
		}
	}
	public void showMatchResultErrorInfo(){
		matchSearchScrollPane.setVisible(false);
		searchInfoLabel.setText("* Any match is not found");
		searchInfoLabel.setVisible(true);
	}
	public void fillTeamComboBox(List<Map> teamsName){
		Object[] items = new Object[teamsName.size()];
	    for(int i = 0; i < items.length; i++){
	    	Map item = teamsName.get(i);
	    	items[i] = item.get("teamName");
	    }
	    DefaultComboBoxModel model = new DefaultComboBoxModel(items);
		teamsComboBox.setModel(model);
	}
	/*public void selectBackToUserMenu(ActionListener l){
		backUserOptionButton.addActionListener(l);
	}*/
	public void addSearchMatchButtonActionListener(ActionListener l){
	   searchMatchButton.addActionListener(l);	
	}
	public void addSearchByTeamRadioButtonActionListener(ActionListener l){
		searchByTeamRadioButton.addActionListener(l);
	}
	public void addSearchByDateRadioButtonActionListener(ActionListener l){
		searchByDateRadioButton.addActionListener(l);
	}
	public void setSearchOptionLabelText(String text){
		  searchOptionLabel.setText(text);
	}
	public void setVisibleTeamComboBox(){
		matchDateText.setVisible(false);  
		teamsComboBox.setVisible(true);
	}
	public void setVisibleMatchDateTetx(){
		teamsComboBox.setVisible(false);
		matchDateText.setVisible(true);
	}
	public JTextField getMatchDateText() {
		return matchDateText;
	}

	public void setMatchDateText(JTextField matchDateText) {
		this.matchDateText = matchDateText;
	}

	public JRadioButton getSearchByTeamRadioButton() {
		return searchByTeamRadioButton;
	}

	public void setSearchByTeamRadioButton(JRadioButton searchByTeamRadioButton) {
		this.searchByTeamRadioButton = searchByTeamRadioButton;
	}

	

	public JRadioButton getSearchByDateRadioButton() {
		return searchByDateRadioButton;
	}

	public void setSearchByDateRadioButton(JRadioButton searchByDateRadioButton) {
		this.searchByDateRadioButton = searchByDateRadioButton;
	}

	public JComboBox getTeamsComboBox() {
		return teamsComboBox;
	}

	public void setTeamsComboBox(JComboBox teamsComboBox) {
		this.teamsComboBox = teamsComboBox;
	}

	public JLabel getSearchOptionLabel() {
		return searchOptionLabel;
	}

	public void setSearchOptionLabel(JLabel searchOptionLabel) {
		this.searchOptionLabel = searchOptionLabel;
	}

	public JButton getSearchMatchButton() {
		return searchMatchButton;
	}

	public void setSearchMatchButton(JButton searchMatchButton) {
		this.searchMatchButton = searchMatchButton;
	}

	public JTable getMatchSearchScrollTable() {
		return matchSearchScrollTable;
	}

	public void setMatchSearchScrollTable(JTable matchSearchScrollTable) {
		this.matchSearchScrollTable = matchSearchScrollTable;
	}
	
	
	public JLabel getSearchInfoLabel() {
		return searchInfoLabel;
	}


	public void setSearchInfoLabel(JLabel searchInfoLabel) {
		this.searchInfoLabel = searchInfoLabel;
	}


	public JScrollPane getMatchSearchScrollPane() {
		return matchSearchScrollPane;
	}


	public void setMatchSearchScrollPane(JScrollPane matchSearchScrollPane) {
		this.matchSearchScrollPane = matchSearchScrollPane;
	}


	public ActionListener getGoMatchDetailButton() {
		return goMatchDetailButtonActionListener;
	}


	public void setGoMatchDetailButton(ActionListener goMatchDetailButton) {
		this.goMatchDetailButtonActionListener = goMatchDetailButton;
	}


	public ButtonGroup getButtonGroup() {
		return buttonGroup;
	}


	public class ButtonColumn extends AbstractCellEditor
	implements TableCellRenderer, TableCellEditor, MouseListener
{
	private JTable table;
	private AbstractAction action;
	private int mnemonic;
	private Border originalBorder;
	private Border focusBorder;

	private JButton renderButton;
	private JButton editButton;
	private String text;
	private boolean isButtonColumnEditor;

	/**
	 *  Create the ButtonColumn to be used as a renderer and editor. The
	 *  renderer and editor will automatically be installed on the TableColumn
	 *  of the specified column.
	 *
	 *  @param table the table containing the button renderer/editor
	 *  @param action the Action to be invoked when the button is invoked
	 *  @param column the column to which the button renderer/editor is added
	 */
	public ButtonColumn(JTable table, int column)
	{
		this.table = table;
		this.action = action;

		renderButton = new JButton();
		editButton = new JButton();
		editButton.setFocusPainted( false );
		//editButton.addActionListener( this );
		originalBorder = editButton.getBorder();
		setFocusBorder( new LineBorder(Color.BLUE) );

		TableColumnModel columnModel = table.getColumnModel();
		columnModel.getColumn(column).setCellRenderer( this );
		columnModel.getColumn(column).setCellEditor( this );
		//table.addMouseListener( this );
	}


	/**
	 *  Get foreground color of the button when the cell has focus
	 *
	 *  @return the foreground color
	 */
	public Border getFocusBorder()
	{
		return focusBorder;
	}

	/**
	 *  The foreground color of the button when the cell has focus
	 *
	 *  @param focusBorder the foreground color
	 */
	public void setFocusBorder(Border focusBorder)
	{
		this.focusBorder = focusBorder;
		editButton.setBorder( focusBorder );
	}

	public int getMnemonic()
	{
		return mnemonic;
	}

	/**
	 *  The mnemonic to activate the button when the cell has focus
	 *
	 *  @param mnemonic the mnemonic
	 */
	public void setMnemonic(int mnemonic)
	{
		this.mnemonic = mnemonic;
		renderButton.setMnemonic(mnemonic);
		editButton.setMnemonic(mnemonic);
	}

	@Override
	public Component getTableCellEditorComponent(
		JTable table, Object value, boolean isSelected, int row, int column)
	{
		text = (value == null) ? "" : value.toString();
		editButton.setText( text );
		return editButton;
	}

	@Override
	public Object getCellEditorValue()
	{
		return text;
	}

//
//  Implement TableCellRenderer interface
//
	public Component getTableCellRendererComponent(
		JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
	{
		if (isSelected)
		{
			renderButton.setForeground(table.getSelectionForeground());
	 		renderButton.setBackground(table.getSelectionBackground());
		}
		else
		{
			renderButton.setForeground(table.getForeground());
			renderButton.setBackground(UIManager.getColor("Button.background"));
		}

		if (hasFocus)
		{
			renderButton.setBorder( focusBorder );
		}
		else
		{
			renderButton.setBorder( originalBorder );
		}

		renderButton.setText( (value == null) ? "" : value.toString() );
		return renderButton;
	}
	
	public void addActionListenerEditButton(ActionListener l){
		editButton.addActionListener(l);
	}
    public void setCommand(int i){
    	editButton.setActionCommand(String.valueOf(i));
    }
//
//  Implement ActionListener interface
//
	/*
	 *	The button has been pressed. Stop editing and invoke the custom Action
	 */
	
	/*public void actionPerformed(ActionEvent e)
	{
		int row = table.convertRowIndexToModel( table.getEditingRow() );
		fireEditingStopped();

	
       
		ActionEvent event = new ActionEvent(
			table,
			ActionEvent.ACTION_PERFORMED,
			"" + row);
		 action.actionPerformed(event);
	}
*/
//
//  Implement MouseListener interface
//
	/*
	 *  When the mouse is pressed the editor is invoked. If you then then drag
	 *  the mouse to another cell before releasing it, the editor is still
	 *  active. Make sure editing is stopped when the mouse is released.
	 */
    public void mousePressed(MouseEvent e)
    {
    	if (table.isEditing()
		&&  table.getCellEditor() == this)
			isButtonColumnEditor = true;
    	
    }

    public void mouseReleased(MouseEvent e)
    {
    	if (isButtonColumnEditor
    	&&  table.isEditing())
    		table.getCellEditor().stopCellEditing();

		isButtonColumnEditor = false;
    }

    public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}

}
